/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.reminder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

import org.niclab.vanbus.R;
import org.niclab.vanbus.activity.comp.StartStopReminderDialog;
import org.niclab.vanbus.activity.controller.BusStopController;
import org.niclab.vanbus.activity.controller.ReminderController;
import org.niclab.vanbus.database.ApplicationDataBase;
import org.niclab.vanbus.database.ReminderDAO;
import org.niclab.vanbus.model.Reminder.ReminderType;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class StartStopReminderReceiver extends BroadcastReceiver{
	
	private static final String LOG_TAG="StartStopReminderReceiver";
	//private static final int START_STOP_NOTIFICATION= 1;
	

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d(LOG_TAG,"received an alarm broadcast");
		ArrayList<String> times = intent.getStringArrayListExtra(ReminderController.REMINDER_START_STOP_ALARM_INTENT_KEY_BUS_TIMES);
		String stopName = intent.getStringExtra(ReminderController.REMINDER_START_STOP_ALARM_INTENT_KEY_BUS_STOP_NAME);
		String stopId = intent.getStringExtra(ReminderController.REMINDER_START_STOP_ALARM_INTENT_KEY_BUS_STOP_ID);
		String routeId = intent.getStringExtra(ReminderController.REMINDER_START_STOP_ALARM_INTENT_KEY_ROUTE_ID);
		
		Log.v(LOG_TAG,"start stop alarm: stopName:" +stopName + "stopId: "+stopId + " routeId: "+routeId+" Times: "+ times.toString());
		
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		
		int icon = R.drawable.bus;
		CharSequence tickerText = "Catch Bus!";
		long when = System.currentTimeMillis();         // notification time
		
		
		Notification notification = new Notification(icon, tickerText,when);
		notification.defaults |=Notification.DEFAULT_VIBRATE;
		notification.defaults |=Notification.DEFAULT_SOUND;
		
		notification.flags |= Notification.FLAG_INSISTENT;
		RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.reminder_start_stop_notification);
		contentView.setImageViewResource(R.id.reminder_notification_start_stop_img, R.drawable.bus);
		
		String timeText = new SimpleDateFormat("hh:mm a").format(new Date(when));
		contentView.setTextViewText(R.id.reminder_notification_start_stop_time, timeText);
		
		CharSequence contentText ="Next bus is coming to " +stopName+" at "+ times.toString(); 
		contentView.setTextViewText(R.id.reminder_notification_start_stop_message, contentText);
	
		
		CharSequence contentTitle="Time to catch your bus "+routeId;  // expanded message title
		contentView.setTextViewText(R.id.reminder_notification_start_stop_title, contentTitle);

		notification.contentView = contentView;
		

		
		
		Intent notificationIntent = new Intent();
		
		PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);

		notification.contentIntent = contentIntent;

		mNotificationManager.notify(new Random().nextInt(), notification);
		
		//remove the reminder from database
		ReminderController reminderCtr = new ReminderController(context);

		Log.v(LOG_TAG,"the Reminder_ID of the reminder to remove:" + intent.getData().getLastPathSegment());
		reminderCtr.deleteReminderByIdFromDB(ReminderType.StartStopReminder, intent.getData().getLastPathSegment());

		

	}
	

}
